<?php
/* MyMeeting is an online meeting management software.
 * Copyright (C) 2009 Government Of Malaysia
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * @author: Abdullah Zainul Abidin, Nuhaa All Bakry
 *          Eavay Javay Barnad, Sarogini Muniyandi, Teow Jit Huan
 *
 */
class Userstatus extends AppModel {

/**
 * Defining the name of model
 *
 */
    var $name = 'Userstatus';
/**
 * Defining the name of the table
 *
 */
    var $useTable = 'userstatuses';
/**
 * Defining behavior in the model
 *
 */
    var $actsAs = array('Comment','Workflow','MultiFile');

    //The Associations below have been created with all possible keys, those that are not needed can be removed
/**
 * Building assosiations betweeen models 
 *
 */
    var $belongsTo = array(
        'Committee' => array(
            'className' => 'Committee',
            'foreignKey' => 'committee_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''),
        'Decision' => array(
            'className' => 'Decision',
            'foreignKey' => 'decision_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''),
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''),
        'Updater' => array(
            'className' => 'User',
            'foreignKey' => 'updater',
            'conditions' => '',
            'fields' => '',
            'order' => '')
        );
    
    function getHistory($users,$dec_id) {
        $status = $this->find('all',array(
            'conditions'=>array('Userstatus.user_id'=>$users,'Userstatus.decision_id'=>$dec_id),
            'order'=>'Userstatus.created DESC',
        ));
        return $status;
    }
    
    function beforeSave(){
        if(!empty($this->data['Userstatus']['description'])){
            $a=$this->data['Userstatus']['description'];
            // convert h1-6 to strong
            $hfront=array('<h1>','<h2>','<h3>','<h4>','<h5>','<h6>');
            $hback=array('</h1>','</h2>','</h3>','</h4>','</h5>','</h6>');
            $a=str_replace($hfront,'<strong>',$a);
            $a=str_replace($hback,'</strong>',$a);
            // strip the page margin format
            while(strpos($a,'&lt;!--')){
                $b=strpos($a,'&lt;!--');
                $d=strpos($a,'--&gt;',$b);
                $a=str_replace(substr($a,$b,$d-$b+6),'',$a);
            }
            //strip the font-size format
            while(strpos($a,'font-size')){
                $c=strpos($a,'font-size');
                $d=strpos($a,';',$c);
                $a=str_replace(substr($a,$c,$d-$c+1),'',$a);
            }
            //keep it clean
            $a=str_replace('style=""','',$a);
            $a=str_replace('n >','n>',$a);
            $a=str_replace(array('<p></p>','<p>&nbsp;</p>','</span><span>','<span></span>'),'',$a);
            $this->data['Userstatus']['description']=$a;
        }
        return true;
    }
}
?>
